import {
  Controller,
  Get,
  Param,
  Post,
  Body,
  Delete,
  Query,
} from '@nestjs/common';
import { VideoInfoService } from './video/video.service';
import { VideoRecordService } from './videoRecord/videoRecord.service';
import {
  VideoInfo as VideoInfoModel,
  VideoRecord as VideoRecordModel,
} from '@prisma/client';

export interface PageResponse<T> {
  current: number;
  data: T[];
  pageSize: number;
  success: boolean;
  total: number;
}

@Controller()
export class AppController {
  constructor(
    private readonly videoInfoService: VideoInfoService,
    private readonly videoRecordService: VideoRecordService,
  ) {}

  @Get('video/:id')
  async getVideo(@Param('id') id: string): Promise<VideoInfoModel> {
    return this.videoInfoService.videoInfo({ id: Number(id) });
  }

  @Get('filtered-videos/:searchString')
  async getFilteredVideos(
    @Param('searchString') searchString: string,
  ): Promise<PageResponse<VideoInfoModel>> {
    console.log('filtered page');
    return this.videoInfoService.videoInfos({
      where: {
        status: Number(searchString),
      },
    });
  }
  @Get('videos')
  async getVideos(
    @Query('pageSize') pageSize: number = 0,
    @Query('current') current: number = 0,
  ): Promise<PageResponse<VideoInfoModel>> {
    // const { pageSize = 0, current = 0 } = queryData;
    const size = Number(pageSize);
    return this.videoInfoService.videoInfos({
      skip: (current - 1) * size,
      take: size,
    });
  }

  @Post('record')
  async createVideoRecord(
    @Body()
    postData: {
      platform: number;
      harf: string;
      type: number;
      videoId: number;
      likes: number;
      views: number;
      comments: number;
      collections: number;
    },
  ): Promise<VideoRecordModel> {
    const {
      platform,
      harf,
      type,
      videoId,
      likes,
      comments,
      views,
      collections,
    } = postData;
    return this.videoRecordService.createVideoRecord({
      platform,
      harf,
      type,
      video: {
        connect: {
          id: videoId,
        },
      },
      likes,
      comments,
      views,
      collections,
    });
  }

  @Post('video')
  async createVideo(
    @Body() userData: { title: string; summary: string; status: number },
  ): Promise<VideoInfoModel> {
    return this.videoInfoService.createVideoInfo(userData);
  }

  @Delete('video/:id')
  async deleteVideo(@Param('id') id: string): Promise<VideoInfoModel> {
    return this.videoInfoService.deleteVideoInfo({ id: Number(id) });
  }
}
